package logic

import (
	"context"
	"fmt"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *personrpc.RegisterRequest) (*personrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var finduse mysql.Use
	mysql.DB.Debug().Where("account=?", in.Account).Find(&finduse)
	if finduse.ID != 0 {
		return nil, fmt.Errorf("此账号已注册，请重新登录")
	}
	use := mysql.Use{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&use)
	return &personrpc.RegisterResponse{
		Id: int64(use.ID),
	}, nil
}
